<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProductCategories extends Model
{
    // use SoftDeletes;
	protected $primaryKey = 'product_category_id';

    protected $fillable = ['name', 'level', 'pid', 'sort', 'created_at', 'updated_at'];

    public function hasChildren()
    {
    	return $this->hasMany('App\ProductCategories', 'pid', 'product_category_id');
    }

    /**
     * 分类树
     * @return 分类树
     */
    public function categoryTree($parents)
    {
        $tree = [];

        foreach ($parents as $k=>$parent) {
            $childs = $parent->hasChildren;

            if (count($childs) > 0) {
                $childs = $this->categoryTree($childs);
            }

            $parent['childs'] = $childs;
            $tree[] = $parent;
        }
        return $tree;
    }

    /**
     * 无限分类
     * @param  array $parents 传入父类
     * @return 二维数组
     */
    public function getChilds($parents)
    {
    	$all = [];

        foreach ($parents as $k=>$parent) {
            $childs = $parent->hasChildren;

            if (count($childs) > 0) {
                $childs = $this->getChilds($childs);
                $all = array_merge($all, $childs);
            }
            $all[] = $parent;
        }
        return $all;
    }
}
